
/**
 * 通用js方法封装处理  utils
 */

// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null;
  }
  const format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    } else if (typeof time === "string") {
      time = time
        .replace(new RegExp(/-/gm), "/")
        .replace("T", " ")
        .replace(new RegExp(/\.[\d]{3}/gm), "");
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}

// 表单重置
export function resetForm(refName) {
  if (this.$refs[refName]) {
    this.$refs[refName].resetFields();
  }
}

// 回显数据字典
export function selectDictLabel(datas, value) {
  var actions = [];
  Object.keys(datas).some((key) => {
    if (datas[key].value == "" + value) {
      actions.push(datas[key].label);
      return true;
    }
  });
  return actions.join("");
}


/**
 * @function 查询数组方法
 * @param {Array} data 数组
 * @param {String} key 查询 key
 * @param {Array} valueAyy 查询数组
 * @return Array
 */
export function arrayDiff(data, key, valueAyy) {
  return data.filter((prams) => valueAyy.indexOf(prams[key]) !== -1);
}

/**
 * @function 查询树形结构数据
 * @param {Array | String} objQuery 关键字
 * @param {Array} data 要过滤的数组
 * @param {String} key 过滤的key
 * @return Array
 */
export function searchTree(
  objQuery,
  data,
  key = "id",
  children = "children",
  newArr = []
) {
  data.forEach((element) => {
    if (typeof objQuery === "string" || typeof objQuery === "number") {
      if (objQuery == element[key]) {
        newArr.push(element);
      }
    } else {
      if (objQuery.indexOf(element[key]) > -1) {
        newArr.push(element);
      }
    }
    if (element[children] && element[children].length > 0) {
      searchTree(objQuery, element[children], key, children, newArr);
    }
  });
  return newArr;
}
/**
 * @function 删除数组对象中某一项
 * @param {Array} arr 对象
 * @param {String} id 删除值为
 * @param {String} key key
 */
export function findIndex(arr, id, key = "id") {
  if (arr && arr.length && arr.findIndex((item) => item[key] === id) !== -1) {
    return arr.splice(
      arr.findIndex((item) => item[key] === id),
      1
    );
  } else {
    return arr;
  }
}


/**
 * @function 数组去重
 * @param {Array} arr 数组
 * @param {String} key key
 * @return Array
 */
export function distinct(arr, key) {
  var newObj = {},
    newArr = [];
  if (key) {
    arr.forEach((item) => {
      if (!newObj[item[key]]) newObj[item[key]] = newArr.push(item);
    });
  } else {
    arr.forEach((item) => {
      if (newArr.indexOf(item) == -1) newArr.push(item);
    });
  }
  return newArr;
}

/**
 * @function 数组合并去重
 * @param {Array} arr1 数组1
 * @param {Array} arr2 数组2
 * @param {String} key key
 * @return Array
 */
export function merge(arr1, arr2, key = "id") {
  let json = arr2.concat(arr1);
  return distinct(json, key);
}

// js删除对象/数组中null、undefined、空对象及空数组方法
export function delEmptyQueryNodes(data = {}) {
  return Object.keys(data)
    .filter((key) => data[key] !== null && data[key] !== undefined && data[key] !== '')
    .reduce((acc, key) => ({ ...acc, [key]: data[key] }), {});
}
